package com.mariusz.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import org.jdesktop.swingx.JXTitledPanel;

import com.mariusz.data.dto.Property;
import com.mariusz.data.dto.Rentier;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.ui.models.LocatorsTableModel;

import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JSeparator;

public class LocatorsPanel  extends JXTitledPanel implements ActionListener, MouseListener {
	private JTable locatorsTable;
	private JLabel lblPropertyName;
	private JLabel lblPropertyAddress;
	private JLabel lblPropertyRoomsCount;
	private JLabel lblRentierName;
	private JLabel lblRentierId;
	private JLabel lblRentierTelephone;
	private JLabel lblRentierLastPayment;
	private int clickedRentier = -1;
	
	private JButton btnBack;
	
	public LocatorsPanel() {
		setTitle(" Locators:");
		getContentContainer().setLayout(null);
		
		ResourceImagePanel resourceImagePanel = new ResourceImagePanel("Courtyard-Naworth-Castle-small.jpg");
		resourceImagePanel.setBounds(10, 11, 366, 247);
		getContentContainer().add(resourceImagePanel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 263, 452, 154);
		getContentContainer().add(scrollPane);
		
		locatorsTable = new JTable();
		scrollPane.setViewportView(locatorsTable);
		locatorsTable.addMouseListener(this);
		btnBack = new JButton("Back");
		btnBack.setBounds(491, 391, 164, 26);
		btnBack.addActionListener(this);
		getContentContainer().add(btnBack);
		
		lblPropertyAddress = new JLabel();
		lblPropertyAddress.setFont(new Font("Tahoma", Font.ITALIC, 11));
		lblPropertyAddress.setText("property address");
		lblPropertyAddress.setBounds(386, 30, 239, 14);
		getContentContainer().add(lblPropertyAddress);
		
		lblPropertyName = new JLabel();
		lblPropertyName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPropertyName.setText("property name");
		lblPropertyName.setBounds(386, 11, 261, 14);
		getContentContainer().add(lblPropertyName);
		
		lblPropertyRoomsCount = new JLabel();
		lblPropertyRoomsCount.setFont(new Font("Tahoma", Font.ITALIC, 11));
		lblPropertyRoomsCount.setText("property rooms");
		lblPropertyRoomsCount.setBounds(386, 49, 239, 14);
		getContentContainer().add(lblPropertyRoomsCount);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(386, 74, 291, 2);
		getContentContainer().add(separator);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(btnBack == arg0.getSource()){
			NavigationManager.getInstance().switchTo(Screen.PROPERTIES);
		}
	}
	
	@Override
	public void show(boolean arg0) {
		fillData();
		
		super.show(arg0);
	}
	
	private void fillData(){
		Property property = DataManager.getInstance().getCurrentProperty();
		if(property == null){
			return;
		}
		lblPropertyName.setText(property.getName());
		lblPropertyAddress.setText(property.getAddress());
		lblPropertyRoomsCount.setText("Total number of rooms: "+property.getRooms().size());
		
		// list
		List<Rentier> locators = new ArrayList<Rentier>();
		List<Rentier> rentiers = DataManager.getInstance().getRentiers();
		for(Rentier rentier : rentiers){
			if(property.equals(rentier.getProperty())){
				locators.add(rentier);
			}
		}
		locatorsTable.setModel(new LocatorsTableModel(locators));
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getSource() == locatorsTable){
			int selected = locatorsTable.getSelectedRow();
			if(selected >= 0){
				if(clickedRentier == selected){
					Rentier r = ((LocatorsTableModel)locatorsTable.getModel()).getAt(selected);
					DataManager.getInstance().setCurrentRentier(r);
					NavigationManager.getInstance().switchTo(Screen.RENTIER_DETAILS);
					
				}else{
					clickedRentier = selected;
				}
			}
		}
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
